package org.zuel.huhuForum.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.zuel.huhuForum.param.Event;
import org.zuel.huhuForum.util.ResponseUtil;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MainService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    public Object commonEmail(Event event) {
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(from);
        //谁要接收
        message.setTo((String) event.getData().get("email"));
        //邮件标题
        message.setSubject((String) event.getData().get("subject"));
        //邮件内容
        message.setText((String) event.getData().get("text"));
        try {
            mailSender.send(message);
            return ResponseUtil.ok(event.getData().get("email") + "发送普通邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            return ResponseUtil.fail("普通邮件方失败");
        }
    }

    public  Object htmlMail(Event event) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //true 表⽰示需要创建⼀一个 multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo((String) event.getData().get("email"));
        helper.setSubject((String) event.getData().get("subject"));
        String text=getLogin(event , event.getEntityType());
        helper.setText(text, true);
//        MimeBodyPart image3 = new MimeBodyPart();
//        // 读取本地文件
//        DataHandler dh = new DataHandler(new FileDataSource("src/main/java/org/zuel/huhuForum/component/Mailpng/3.png"));
//        // 将图片数据添加到"节点"
//        image3.setDataHandler(dh);
//        // 为"节点"设置一个唯一编号（在文本"节点"将引用该ID）
//        image3.setContentID("3");
//
//        MimeBodyPart image222 = new MimeBodyPart();
//        // 读取本地文件
//        DataHandler dh1 = new DataHandler(new FileDataSource("src/main/java/org/zuel/huhuForum/component/Mailpng/222.png"));
//        // 将图片数据添加到"节点"
//        image222.setDataHandler(dh1);
//        // 为"节点"设置一个唯一编号（在文本"节点"将引用该ID）
//        image222.setContentID("222");
//        MimeMultipart mm_text_image = new MimeMultipart();
//
//        MimeBodyPart text = new MimeBodyPart();
//        text.setContent(getLogin(event, event.getEntityType()), "text/html;charset=UTF-8");
//
//        mm_text_image.addBodyPart(text);
//        mm_text_image.addBodyPart(image3);
//        mm_text_image.addBodyPart(image222);
//        mm_text_image.setSubType("related");
//
//        message.setContent(mm_text_image);
//        message.setSentDate(new Date());
        try {
            mailSender.send(message);
            return ResponseUtil.ok(event.getData().get("email") + "发送邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            return ResponseUtil.fail("邮件发送失败");
        }
    }

    private String getLogin(Event event,Integer type){
        String text;
//        text="<!DOCTYPE html>\n" +
//                "<html lang=\"en\">\n" +
//                "<head>\n" +
//                "    <meta charset=\"UTF-8\">\n" +
//                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
//                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
//                "    <title>知乎</title>\n" +
//                "</head>\n" +
//                "\n" +
//                "<body>\n" +
//                "    <div style=\"width: 90%;margin: 20px auto;background-color: white;box-shadow: 5px 5px 5px #999999;\">\n" +
//                "        <div style=\"padding: 20px 50px;\">\n" +
//                "            <img width=\"50px\" height=\"50px\" src='cid:3' alt=\"\">\n" +
//                "            <div>\n" +
//                "                <div style=\"display: inline-block;\">\n" +
//                "                    <h1 style=\"margin-top: 30px;\">「尊敬的用户」</h1>\n" +
//                "                    <h1 style=\"margin-top: 30px;\">「您的账号<span style=\"font-size: 22px;\">"+ event.getData().get("email")+"</span>,"+event.getData().get("subject")+"的验证码为」\n" +
//                "                    </h1>\n" +
//                "                    <div style=\"margin-top: 30px;margin-left:20px;width:100px;height: 51px;padding: 10px 30px;background-color:#056736 ;\">\n" +
//                                      event.getData().get("text")+
//                "                    </div>\n" +
//                "                </div>\n" +
////                "                <img style=\"display: inline-block;vertical-align: top;\" height=\"400px\" width=\"250px;margin-left:60%\"\n" +
////                "                    src='cid:222' alt=\"\">\n" +
//                "            </div>\n" +
//                "        </div>\n" +
//                "        <hr style=\"margin: 20px 30px;\">\n" +
//                "        <div style=\"text-align: center;padding-bottom: 20px;\">\n" +
//                "            <div style=\"display: inline-block;align-items: center;margin: 0 auto;height: 40px;\">\n" +
//                "                <img style=\"vertical-align: middle;\" width=\"40px\" height=\"40px\" src='cid:222' alt=\"\">\n" +
//                "                <div style=\"display: inline-block;color: #cdcdcd;\"> 本服务由“分享我刚编的故事”团队提供</div>\n" +
//                "            </div>\n" +
//                "        </div>\n" +
//                "    </div>\n" +
//                "</body>\n" +
//                "</html>\n" +
//                "<style>\n" +
//                "    * {\n" +
//                "        margin: 0;\n" +
//                "        padding: 0;\n" +
//                "    }\n" +
//                "\n" +
//                "    body {\n" +
//                "        background-color: #f5f5f5;\n" +
//                "    }\n" +
//                "</style>";
        if(type==1){
            text="<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                    "    <title>E-mail</title>\n" +
                    "    <style>\n" +
                    "        div {color: grey; width: 600px;margin: auto;background-color: rgb(250, 250, 250);border-radius: 8px;padding-bottom: 10px;}\n" +
                    "        h1 {margin: 0 0 10px 0;padding: 10px 0 10px 0;border-bottom: solid 1px #ddd;font-size: 20px;text-align: center;}\n" +
                    "        table {width: 90%;margin: 0 20px 15px 20px;}\n" +
                    "        td {text-align: center; padding: 5px;line-height: 1.6;width: 50%;}\n" +
                    "        p {font-size: 16px;text-align: center;margin-bottom:10px;}\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <div>\n" +
                    "        <h1>"+event.getData().get("subject")+"确认</h1>\n" +
                    "        <table class=\"table table-striped table-hover\">\n" +
                    "            <tbody>\n" +
                    "                <tr>\n" +
                    "                    <td>用户邮箱:</td>\n" +
                    "                    <td>"+event.getData().get("email")+"</td>\n" +
                    "                </tr>\n" +
                    "                <tr>\n" +
                    "                    <td>处理时间:</td>\n" +
                    "                    <td>"+event.getData().get("time")+"</td>\n" +
                    "                </tr>\n"+
                    "                <tr>\n" +
                    "                    <td>验证码:</td>\n" +
                    "                </tr>\n" +
                    "            </tbody>\n" +
                    "        </table>\n" +
                    "        <h1>"+event.getData().get("text")+"</h1>\n" +
                    "        <p>感谢您使用</p>\n" +
                    "        <p>如果这不是您的操作，请忽略...</p>\n" +
                    "    </div>\n" +
                    "</body>\n" +
                    "</html>";
        }else {
            text="<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                    "    <title>E-mail</title>\n" +
                    "    <style>\n" +
                    "        div {color: grey; width: 600px;margin: auto;background-color: rgb(250, 250, 250);border-radius: 8px;padding-bottom: 10px;}\n" +
                    "        h1 {margin: 0 0 10px 0;padding: 10px 0 10px 0;border-bottom: solid 1px #ddd;font-size: 20px;text-align: center;}\n" +
                    "        table {width: 90%;margin: 0 20px 15px 20px;}\n" +
                    "        td {text-align: center; padding: 5px;line-height: 1.6;width: 50%;}\n" +
                    "        p {font-size: 16px;text-align: center;margin-bottom:10px;}\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "    <div>\n" +
                    "        <h1>"+event.getData().get("subject")+"确认</h1>\n" +
                    "        <table class=\"table table-striped table-hover\">\n" +
                    "            <tbody>\n" +
                    "                <tr>\n" +
                    "                    <td>用户编号</td>\n" +
                    "                    <td>"+event.getUserId()+"</td>\n" +
                    "                </tr>\n" +
                    "                <tr>\n" +
                    "                    <td>用户邮箱:</td>\n" +
                    "                    <td>"+event.getData().get("email")+"</td>\n" +
                    "                </tr>\n" +
                    "                <tr>\n" +
                    "                    <td>处理时间:</td>\n" +
                    "                    <td>"+event.getData().get("time")+"</td>\n" +
                    "                </tr>\n"+
                    "            </tbody>\n" +
                    "        </table>\n" +
                    "        <p>感谢您使用</p>\n" +
                    "        <p>如果这不是您的操作，请立即修改信息...</p>\n" +
                    "    </div>\n" +
                    "</body>\n" +
                    "</html>";
        }

        return text;

    }

}
